package MyThread;

import com.sun.xml.internal.ws.util.StringUtils;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class LogManage {
    public static void main(String[] args) {
        LogManage logManage = new LogManage();
        Scanner input = new Scanner(System.in);
        while (true) {
            String line = input.nextLine();
            logManage.info(line);
        }
    }

    private static final BlockingDeque<String> blockingDeque = new LinkedBlockingDeque<>();
    private static String FILEDIR = "C:\\Users\\Administrator\\Desktop\\work\\log.txt";

    private LogThread logThread;

    public LogManage() {
        logThread = new LogThread();
        logThread.start();
    }

    public void info(String message) {
        blockingDeque.offer(message);
    }

    class LogThread extends Thread {
        @Override
        public void run() {
            while (true) {
                String message = blockingDeque.poll();
                if (null != message && !("").equals(message)) {
                    write(FILEDIR, message);
                }
            }
        }
    }

    public static void write(String file, String conent) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true)));
            out.write(conent + "\r\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
